package utils;

import com.ctrip.framework.apollo.ConfigService;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

@Slf4j
public class AESUtil {

    private final static String KEY = ConfigService.getAppConfig().getProperty(ApolloUtils.KEY, ApolloUtils.KEY_DEFAULT);

    private static byte[] getSecretKey(String key, int len) {
        byte[] keyBytes;
        try {
            keyBytes = key.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
        byte[] newBytes = new byte[16];
        // newBytes.length = len;
        int keyLen = key.length();
        if (keyLen > len)
            return null;
        int leftLen = len - keyLen;
        for (int i = 0; i < keyLen; i++) {
            newBytes[i] = keyBytes[i];
        }
        for (int j = 0; j < leftLen; j++) {
            newBytes[keyLen + j] = 0;
        }
        return newBytes;
    }

    public static String encrypt(String message) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            byte[] keyBytes = getSecretKey(KEY, 16);

            if (keyBytes == null) {
                return null;
            }
            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
            byte[] ivb = {0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
                    0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30};

            IvParameterSpec iv = new IvParameterSpec(ivb);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);

            return Base64.getEncoder().encodeToString(cipher.doFinal(message.getBytes("UTF-8")));
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String message) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byte[] keyBytes = getSecretKey(KEY, 16);

            if (keyBytes == null) {
                return null;
            }

            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
            byte[] ivb = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

            IvParameterSpec iv = new IvParameterSpec(ivb);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);

            byte[] buffer = Base64.getDecoder().decode(message);
            byte[] utf8 = cipher.doFinal(buffer);

            return new String(utf8, "UTF8");
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;

    }

}

